﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using XMLCharSheets;
using XMLCharSheets.Annotations;

namespace CombatAutomationTheater
{
    [DataContract]
    public class PathfinderLightType : INotifyPropertyChanged
    {

        private LightType _currentLightType;
        private string _lightDescription;

        public enum LightType
        {
            NoneSet,
            Dark,
            Dim,
            Normal,
            Bright,
            Disabled
        }
        public PathfinderLightType(LightType theType)
        {
            CurrentLightType = theType;
        }

        [DataMember]
        public LightType CurrentLightType
        {
            get { return _currentLightType; }
            private set { _currentLightType = value; }
        }

        public PathfinderLightType IncreaseOneStep()
        {
            LightType newval = LightType.Normal;
            switch (CurrentLightType)
            {
                case LightType.Normal:
                    newval = LightType.Bright;
                    break;
                case LightType.Bright:
                    newval = LightType.Bright;
                    break;
                case LightType.Dark:
                    newval = LightType.Dim;
                    break;
                case LightType.Dim:
                    newval = LightType.Normal;
                    break;
            }
            PathfinderLightType ret = new PathfinderLightType(newval);
            return ret;
        }

        public PathfinderLightType CreateLightByStep(int stepChange)
        {
            int curStep = stepChange;
            PathfinderLightType ret = new PathfinderLightType(this.CurrentLightType);
            if (stepChange > 0)
            {
                while (curStep>0)
                {
                    ret = ret.IncreaseOneStep();
                    curStep--;
                }
            }
            else
            {
                while (curStep < 0)
                {
                    ret = ret.DecreaseOneStep();
                    curStep++;
                }
            }
            return ret;
        }
        public override string ToString()
        {
            return _currentLightType.ToString();
        }

        public PathfinderLightType DecreaseOneStep()
        {
            LightType newval = LightType.Normal;
            switch (CurrentLightType)
            {
                case LightType.Normal:
                    newval = LightType.Dim;
                    break;
                case LightType.Bright:
                    newval = LightType.Normal;
                    break;
                case LightType.Dark:
                    newval = LightType.Dark;
                    break;
                case LightType.Dim:
                    newval = LightType.Dark;
                    break;
            }
            PathfinderLightType ret = new PathfinderLightType(newval);
            return ret;
        }

        public static PathfinderLightType Brighter(PathfinderLightType a, PathfinderLightType b)
        {
            int aVal = GetVal(a.CurrentLightType);
            int bVal = GetVal(b.CurrentLightType);
            if (aVal > bVal)
                return new PathfinderLightType(a.CurrentLightType);
            return new PathfinderLightType(b.CurrentLightType);
        }

        private static int GetVal(LightType currentLightType)
        {
            switch (currentLightType)
            {
                case LightType.Dark:
                    return 0;
                case LightType.Bright:
                    return 3;
                case LightType.Normal:
                    return 2;
                case LightType.Dim:
                    return 1;

            }
            return -1;

        }

        /// <summary>
        /// Returns a value of 'true' if this light source produces light greater than the environmental light
        /// </summary>
        public bool IsBrighter(PathfinderLightType environmentalLight)
        {
            var brightest = (Brighter(this, environmentalLight));
            if (brightest.CurrentLightType == environmentalLight.CurrentLightType)
            {
                return false;
            }
            return true;

        }

        public PathfinderLightType Copy()
        {
            return new PathfinderLightType(CurrentLightType);

        }


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}